/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.util;

import ohos.app.Context;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;

/**
 * FileUtils
 */
public class FileUtils {
    /**
     * 转换文件大小
     *
     * @param fileSize 文件大小long
     * @return 文件大小
     */
    public static String formetFileSize(long fileSize) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        String wrongSize = "0B";
        if (fileSize == 0) {
            return wrongSize;
        }
        if (fileSize < 1000) {
            fileSizeString = df.format((double) fileSize) + "B";
        } else if (fileSize < 1048576) {
            fileSizeString = df.format((double) fileSize / 1000) + "KB";
        } else if (fileSize < 1073741824) {
            fileSizeString = df.format((double) fileSize / 1000000) + "MB";
        } else {
            fileSizeString = df.format((double) fileSize / 1000000000) + "GB";
        }
        return fileSizeString;
    }

    /**
     * subSuffix
     * @param fileNameOrPath
     * @return String
     */
    public static String subSuffix(String fileNameOrPath) {
        if (fileNameOrPath == null) {
            return null;
        }

        int dotIndex = fileNameOrPath.lastIndexOf('.');
        if (dotIndex == -1) {
            return null;
        }

        return fileNameOrPath.substring(dotIndex);
    }
}
